﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.MobileControls;
using System.Collections.Generic;

public partial class Agent_Certification : AgentPage
{
    protected List<MatchIns.Model.TCertificationType> CertificationType = new List<MatchIns.Model.TCertificationType>();
    protected void Page_Load(object sender, EventArgs e)
    {
        Agent_Account_Menu.SelectMenu = 1;
        CertificationType = new MatchIns.BLL.TCertificationType().GetList(); 
        if (!IsPostBack)
        {
            CheckUserHasRenZheng();
            BindData();
        }
    }

    protected void BindData()
    {
        list.DataSource = CertificationType;
        list.DataBind();
    }

    protected void CheckUserHasRenZheng()
    {
        pi.PageSize = int.MaxValue;
        pi.PageIndex = 1;
        pi.Where = " where agentid="+UserInfo.UserID.ToString();
        List<MatchIns.Model.TCertification> ds = new MatchIns.BLL.TCertification().GetPageList(pi).List;
        if (ds.Count == 0) return;
        GetPagers();
        if (ds.FindAll(delegate(MatchIns.Model.TCertification d) { return d.IsPass == false; }).Count>0)
        {
            info.Text = "你的证件已提交,正处在审核中...";
            list.Visible = false;
            save.Visible = false;
            return;
        }
        else
        {
            info.Text = "你的证件已提交,已通过审核";
            list.Visible = false;
            save.Visible = false;
            return;
        }
    }

    protected void GetPagers()
    {
        pi.Where = "where AgentID="+UserInfo.UserID.ToString();
        List<MatchIns.Model.TCertification> ds = new MatchIns.BLL.TCertification().GetPageList(pi).List;
        rpPagers.DataSource = ds;
        rpPagers.DataBind();
    }

    protected string GetPaperName(object id)
    {
        MatchIns.Model.TCertificationType model =
            CertificationType.Find(delegate(MatchIns.Model.TCertificationType d) { return d.CertificationTypeID == Convert.ToInt32(id); });
        if (model == null)
            return "";
        else
            return model.Name;
    }
    protected void save_Click(object sender, EventArgs e)
    {
        errors.Items.Clear();
        HttpFileCollection files = Request.Files;
        string[] typeNames = Request.Form["typename"].Split(',');
        string[] typeIds = Request.Form["typeid"].Split(',');
        string[] number=Request.Form["number"].Split(',');
        for (int i = 0; i < files.Count;i++ )
        {
            if (number[i].Trim() == "")
                errors.Items.Add("填写:"+typeNames[i]+",证书编号");
            if (files[i].ContentLength == 0)
                errors.Items.Add("选择:"+typeNames[i]+",扫描图片");
            if(files[i].ContentLength>2*1024*1024)
                errors.Items.Add(typeNames[i]+"的上传图片最大为2M");
        }
        if (errors.Items.Count == 0)
        {
            MatchIns.Model.TCertification model = new MatchIns.Model.TCertification();
            MatchIns.BLL.TCertification bll = new MatchIns.BLL.TCertification();
            for (int i = 0; i < files.Count; i++)
            {
                model.AgentID = UserInfo.UserID;
                model.CertificationTypeID = int.Parse(typeIds[i]);
                model.FileName = MatchIns.Common.Upload.UploadImage(files[i], "~/UploadFile/RenZheng/", 400);
                model.IsPass = false;
                model.Remark = "";
                model.UploadTime = DateTime.Now;
                model.Number = number[i];
                bll.Add(model);
            }
            CheckUserHasRenZheng();
        }
    }
}
